package com.wonder.dto;

public class ReverseList {
    static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val,ListNode next){
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode iterator(ListNode head){
        ListNode pre = null,next;
        ListNode current = head;

        while (current != null){
            //保存下一个指针
            next = current.next;
            //当前节点指向倒转
            current.next = pre;
            //将当前节点保存为前节点
            pre = current;
            //游标下移
            current = next;
        }

        return pre;
    }

    public static ListNode recursion(ListNode value){
        //null is head
        if (value == null || value.next == null){
            return value;
        }

        ListNode newHead = recursion(value.next);
        value.next.next = value;
        value.next = null;

        return newHead;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1,null);
        ListNode node2 = new ListNode(2,node1);
        ListNode node3 = new ListNode(3,node2);
        ListNode node4 = new ListNode(4,node3);
        ListNode node5 = new ListNode(5,node4);

        System.out.printf("node1 = "+ node5.val);
        ListNode node = recursion(node5);
        System.out.printf("node = "+ node.val);

    }

}
